package LeetCode;

public class Offer_13 {
    public static void main(String[] args) {
        System.out.println(movingCount(38,15,9));
    }

    public static int movingCount(int m, int n, int k) {
        int count = 0;
        if (m < n) {
            int temp = m;
            m = n;
            n = temp;
        }
        for (int i = 0; i < m; i++) {
            char[] a = (i + "").toCharArray();
            int temp = 0;
            for (char c : a) {
                temp += (c - '0');
            }
            if (temp <= k) {
                count++;
            }else{
                break;
            }
        }
        System.out.println(count);
        if(count==m){
            count*=n;
        }else if(n>=count){
            for(int i=count-1;i>0;i--){
                count+=i;
            }
        }else{
            int temp=count;
            for(int i=n-1;i>0;i--){
                count+=(--temp);
                System.out.println(count);
            }
        }
        return count;
    }
}
